<?php

function list_pelatihan(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Materi pelatihan'=>array('style'=>'border-bottom:2px solid;width:25%'),   
		'Nama pelatihan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Tgl. Mulai'=>array('style'=>'border-bottom:2px solid;width:15%'),   
		'Peserta'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM jadwal_pelatihan a
	INNER JOIN materi_pelatihan b ON a.materi_pelatihan_id = b. materi_pelatihan_id 
	ORDER BY jadwal_pelatihan_id DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$peserta = count_peserta($ey['jadwal_pelatihan_id']);
		 
		$detailproperty = array(
			'href'=>'index.php?com=pendaftaran_pelatihan&task=detail&id='.$ey['jadwal_pelatihan_id'],						 
			'title'=>'Peserta'
		);
		$detail_property = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'lsv'=>   $ey['materi_pelatihan_label'], 
			'sa'=>   $ey['nama_pelatihan'], 
			'tgl'=>	date("d-m-Y" , strtotime($ey['tanggal_mulai'])),
			'l'=>   position_text_align($peserta , 'center') ,     
			'operasi'=> position_text_align(  $detail_property , 'right'),  
		);
	}	
	$datas = table_rows($row);  
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  5 , false ,$paging );
	 
}


function detail_peserta_pelatihan($id){
	my_set_code_js('

		function confirmDelete(sid){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com=pendaftaran_pelatihan&task=delete&id='.$id.'&sid=\'+sid;
			}
			return false;
		}
	');	
	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),  
		'Nama Karyawan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:60%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM karyawan_pelatihan a 
		INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
		WHERE a.jadwal_pelatihan_id ={$id} 
		ORDER BY b.nik";
 
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$i++; 
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['karyawan_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>    position_text_align($karyawan['nik'],'center') ,  
			'nama'=>   $karyawan['nama'],     
			'operasi'=> position_text_align(  $delete_button , 'right'),  
		);
	}	
	$datas = table_rows($row);  
	 
	$fd = "SELECT * FROM jadwal_pelatihan a 
		INNER JOIN kategori_pelatihan b ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
		INNER JOIN materi_pelatihan  c ON a.materi_pelatihan_id = c.materi_pelatihan_id
		WHERE a.jadwal_pelatihan_id = {$id}";
	$re = my_query($fd);
	$wp = my_fetch_array($re);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value=" Kembali " class="main_button" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'\'"/> 
	<input type="button" value=" Cetak " class="main_button" onclick="javascript:window.open(\'index.php?com='.$_GET['com'].'&task=cetak&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');"/>
	<input type="button" value="Tambah data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=edit&id='.$id.'\' });" class="main_button" />  </div></td></tr>';
	$view .= form_field_display( '<span class="label_form">'.strtoupper($wp['kategori_pelatihan_label']).'</span>'   , "<b>Kategori pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['materi_pelatihan_label']).'</span>'   , "<b>Materi pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['nama_pelatihan']).'</span>'   , "<b>Nama pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'. date('d - m - Y' , strtotime($wp['tanggal_mulai']) ) .' s/d '. date('d - m - Y' , strtotime($wp['tanggal_selesai']) ) .'</span>'   , "<b>Tanggal pelatihan</b>"    ); 
	$view .= form_footer( );
	$view .= '</td></tr></table><br/>'; 
	return  $view . table_builder($header , $datas ,  5 , false );
}


function form_pendaftaran_peserta($id){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(

		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}


		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');
	$fd = "SELECT * FROM jadwal_pelatihan a 
		INNER JOIN kategori_pelatihan b ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
		INNER JOIN materi_pelatihan  c ON a.materi_pelatihan_id = c.materi_pelatihan_id
		WHERE a.jadwal_pelatihan_id = {$id}";
	 
	$re = my_query($fd);
	$wp = my_fetch_array($re);
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.strtoupper($wp['kategori_pelatihan_label']).'</span>'   , "<b>Kategori pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['materi_pelatihan_label']).'</span>'   , "<b>Materi pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($wp['nama_pelatihan']).'</span>'   , "<b>Nama pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'. date('d - m - Y' , strtotime($wp['tanggal_mulai']) ) .' s/d '. date('d - m - Y' , strtotime($wp['tanggal_selesai']) ) .'</span>'   , "<b>Tanggal pelatihan</b>"    ); 
	
	$nama_karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>( isset($_POST['nama_karyawan'] ) ? $_POST['nama_karyawan'] : $fields['nama_karyawan']  ),
		'id'=>'nama_karyawan' , 
		'type'=>'text',
		'size'=>'45' ,
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_field_nama_karyawan = form_dynamic($nama_karyawan);
	
	$view .= form_field_display( $form_field_nama_karyawan , "<b>Nama karyawan</b>"    ); 
	$submit = array(
		'value' =>   ' Tambahkan '   ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );$view .= form_footer( ); 
	return $view;
}   

function submit_pendaftaran_peserta($id){
	$karyawan = $_POST['nama_karyawan'];
	
	$check = check_karyawan_by_nik_nama($karyawan);
	if($check){
		$datas = array(
			'karyawan_id'=>my_type_data_int($check),
			'jadwal_pelatihan_id'=>my_type_data_int($id),
		);
		return my_insert_record( 'karyawan_pelatihan' , $datas );
	}
	return false;
}

function delete_pendaftaran_peserta($jadwal_id, $karyawan_id){
	$query = "DELETE FROM karyawan_pelatihan 
		WHERE karyawan_id={$karyawan_id} 
		AND jadwal_pelatihan_id ={$jadwal_id}";
	return my_query($query);
}


function count_peserta($pelatihan_id){
	$query = "
	SELECT COUNT(*) AS td FROM karyawan_pelatihan 
	WHERE jadwal_pelatihan_id={$pelatihan_id} 
	";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return  $rw['td'];
}


function print_plain($pelatihan_id){
	$fd = "SELECT * FROM jadwal_pelatihan a 
	INNER JOIN kategori_pelatihan b ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
	INNER JOIN materi_pelatihan  c ON a.materi_pelatihan_id = c.materi_pelatihan_id
	WHERE a.jadwal_pelatihan_id = {$pelatihan_id}";
	$re = my_query($fd);
	$wp = my_fetch_array($re);
	$datas = array();  
	$datas[] = array(
		 'k1'=>array("Kategori pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['kategori_pelatihan_label'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Materi pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['materi_pelatihan_label'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Nama pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array($wp['nama_pelatihan'] ,75,STR_PAD_RIGHT),	
	); 	 
	$datas[] = array(
		 'k1'=>array("Tanggal pelatihan" ,23 ,STR_PAD_RIGHT),
		 'k2'=>array(":",2,STR_PAD_RIGHT),	
		 'k3'=>array(date('d - m - Y' , strtotime($wp['tanggal_mulai']) ) .' s/d '. date('d - m - Y' , strtotime($wp['tanggal_selesai']) ) ,75,STR_PAD_RIGHT),	
	); 
	$query = "SELECT * FROM karyawan_pelatihan WHERE jadwal_pelatihan_id ={$pelatihan_id}";
	$result = my_query($query);
	$i=1;
	$datas[] = array(
		 'k1'=>array(str_repeat("-", 100) ,100 ,STR_PAD_LEFT),
	); 
	$datas[] = array(
		 'k1'=>array("NO" ,5 ,STR_PAD_LEFT),
		 'k2'=>array("NIK" ,20,STR_PAD_RIGHT),
		 'k3'=>array("Nama Karyawan" ,75,STR_PAD_RIGHT),
	); 
	$datas[] = array(
		 'k1'=>array(str_repeat("-", 100) ,100 ,STR_PAD_LEFT),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$datas[] = array(
			 'k1'=>array($i ,5 ,STR_PAD_LEFT),
			 'k2'=>array($karyawan['nik'] ,20,STR_PAD_RIGHT),
			 'k3'=>array($karyawan['nama_gelar'] ,40,STR_PAD_RIGHT),
		); 		
	}
	$view = 	str_pad("CETAK PLANNING SCHEDULING PELATIHAN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);	
}
